//
//  BMHPickerView.m
//  BMHVideoMain
//
//  Created by 陈宪栋 on 2017/3/27.
//  Copyright © 2017年 Baomihua. All rights reserved.
//

#import "BMHPickerView.h"
#import <UtilsMacro.h>
#import <Masonry.h>
#import <UIView+Layout.h>

@interface BMHPickerView ()
@property (nonatomic, strong) UITapGestureRecognizer *tap;
@property (nonatomic, strong) UIView *backgroundView;
@property (nonatomic, strong) UIView *foregroundView;
@property (nonatomic, strong) UIButton *takePhoto;
@property (nonatomic, strong) UIButton *selectAlbum;
@property (nonatomic, strong) UIButton *cancle;
@property (nonatomic, strong) UIView *line1;
@property (nonatomic, strong) UIView *line2;
@property (nonatomic, strong) NSArray *titleArray;
@property (nonatomic, assign) CGFloat foregroundHeight;
@property (nonatomic, weak) id<BMHPickerViewDelegate> delegate;
@end

@implementation BMHPickerView

- (instancetype)initWithTitleArray:(NSArray *)titleArray delegate:(id<BMHPickerViewDelegate>)delegate {
    self = [super initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
    if (self) {
        _delegate = delegate;
        _titleArray = titleArray;
        [self setupView];
        self.tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickOut:)];
        [self.backgroundView addGestureRecognizer:self.tap];
    }
    return self;
}

- (void)setupView {
    self.backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
    self.backgroundView.backgroundColor = [UIColor blackColor];
    self.backgroundView.alpha = 0;
    [self addSubview:self.backgroundView];
    
    self.foregroundHeight = self.titleArray.count * 50 - 1 + 5 + 49;
    self.foregroundView = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenHeight, ScreenWidth, self.foregroundHeight)];
    self.foregroundView.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.foregroundView];
    
    for (NSInteger i = 0; i < self.titleArray.count; i++) {
        UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, i * 50, ScreenWidth, 49)];
        btn.tag = i;
        btn.titleLabel.font = [UIFont systemFontOfSize:18];
        [btn setTitle:self.titleArray[i] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(selectAction:) forControlEvents:UIControlEventTouchUpInside];
        [btn setTitleColor:RGB(0, 0, 0) forState:UIControlStateNormal];
        [self.foregroundView addSubview:btn];
        
        if (i < self.titleArray.count - 1) {
            UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, (i + 1) * 49, ScreenWidth, 1)];
            line.backgroundColor = RGB(230, 230, 230);
            [self.foregroundView addSubview:line];
        } else {
            UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, (i + 1) * 49, ScreenWidth, 5)];
            line.backgroundColor = RGB(230, 230, 230);
            [self.foregroundView addSubview:line];
        }
    }
    self.cancle = [[UIButton alloc] initWithFrame:CGRectMake(0, self.foregroundView.height - 49, ScreenWidth, 49)];
    self.cancle.titleLabel.font = [UIFont systemFontOfSize:18];
    [self.cancle addTarget:self action:@selector(selectAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.cancle setTitle:@"取消" forState:UIControlStateNormal];
    [self.cancle setTitleColor:RGB(0, 0, 0) forState:UIControlStateNormal];
    [self.foregroundView addSubview:self.cancle];
}

#pragma mark - EventResponse
- (void)clickOut:(UITapGestureRecognizer *)tap {
    CGFloat currentTop = [tap locationInView:self].y;
    CGFloat timerViewTop = ScreenHeight - self.foregroundView.height;
    if (currentTop < timerViewTop) {
        [self.delegate pickView:self clickedButtonAtIndex:self.titleArray.count + 1];
        [self hide];
    }
}

- (void)show {
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    [window addSubview:self];
    [UIView animateWithDuration:0.3 animations:^{
        self.backgroundView.alpha = 0.35;
        self.foregroundView.frame = CGRectMake(0, ScreenHeight - self.foregroundHeight, ScreenWidth, self.foregroundHeight);
    }];
}

- (void)hide {
    [UIView animateWithDuration:0.3 animations:^{
        self.backgroundView.alpha = 0;
        self.foregroundView.frame = CGRectMake(0, ScreenHeight, ScreenWidth, self.foregroundHeight);
    } completion:^(BOOL finished) {
        if (finished) {
            [self.backgroundView removeGestureRecognizer:self.tap];
            [self removeFromSuperview];
        }
    }];
}


- (void)selectAction:(UIButton *)btn {
    if (btn == self.cancle) {
        if ([self.delegate respondsToSelector:@selector(pickView:clickedButtonAtIndex:)]) {
            [self.delegate pickView:self clickedButtonAtIndex:self.titleArray.count + 1];
            [self hide];
        }
    } else {
        if ([self.delegate respondsToSelector:@selector(pickView:clickedButtonAtIndex:)]) {
            [self.delegate pickView:self clickedButtonAtIndex:btn.tag];
            [self hide];
        }
    }
}

#pragma mark - dealloc
- (void)dealloc {
    BMHLog(@"dealloc %@",[self class]);
}

@end
